<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');


require_once(JPATH_COMPONENT . DS . 'models' . DS . 'score.php');

class ScorecardViewScorecard extends JView {

	function display($tmpl = null) {
		global $option, $mainframe;

		$model =& $this->getModel();
		$scoreid = $model->getScoreId();
		$usergender = $model->getUserGender();

		if ($scoreid) {
			$ids = $model->getRelatedIds();
		}

		$model =& JModel::getInstance('score', 'ScorecardModel');
		//$courseid = $model->getCourseId() !== null ? $model->getCourseId() : $ids['courseid'];
		$countryid = $model->getCountryId() !== null ? $model->getCountryId() : $ids['countryid'];
		$stateid = $model->getStateId() !== null ? $model->getStateId() : $ids['stateid'];

		// Generate country list
		$country_list = $model->getCountries();
		$countries = array();

		if (count($country_list) > 0) {
			for ($i = 0, $c = count($country_list); $i < $c; $i++) {
				$countries[] = array('value' => $country_list[$i]->id, 'text' => $country_list[$i]->name);
			}
		}
		$country_select = JHTML::_('select.genericlist', $countries, 'countryid', 'class="inputbox" onchange="this.form.submit()"' . '', 'value', 'text', $countryid);
		$this->assignRef('country_select', $country_select);

		// Generate state list
		$state_list = $model->getStates($countryid);
		$states = array();
		if (count($state_list) > 0) {
			for ($i = 0, $c = count($state_list); $i < $c; $i++) {
				$states[] = array('value' => $state_list[$i]->id, 'text' => $state_list[$i]->name);
			}
		}
		$state_select = JHTML::_('select.genericlist', $states, 'stateid', 'class="inputbox" onchange="this.form.submit()"' . ' ', 'value', 'text', $stateid);
		$this->assignRef('state_select', $state_select);

		// Generate course list
		$course_list = $model->getCourses($countryid, $stateid);

		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'GET') {
			$courseid = $ids['courseid'];
		} else {
			$courseid = ($model->getCourseId() === null && count($course_list) > 0) ? $course_list[0]->id : $model->getCourseId();
		}

		$courses = array();
		if (count($course_list) > 0) {
			for ($i = 0, $c = count($course_list); $i < $c; $i++) {
				$courses[] = array('value' => $course_list[$i]->id, 'text' => $course_list[$i]->name);
			}
		}
		$course_select = JHTML::_('select.genericlist', $courses, 'courseid', 'class="inputbox"' . ' ', 'value', 'text', $courseid);
		$this->assignRef('course_select', $course_select);

		// Menu item id
		$itemid = JRequest::getVar('Itemid', '', '', 'int');
		$this->assignRef('itemid', $itemid);

		$model =& $this->getModel();
		$scorecard = $model->getScorecard($courseid);

		$this->assignRef('scorecard', $scorecard['scorecard']);
		if (count($course_list) > 0) {
			$this->assignRef('holes', $scorecard['holes']);
		}

		$post = JRequest::get('post');
		if ($scoreid && $courseid) {
			$score = $model->getScore($scoreid);
			$score_holes = $usergender == 1 ? $score['scores']['player2'] : $score['scores']['player1'];
			if ($score && !$post) {
				$post['ins'] = $score['scoreinfo']['ins'];
				$post['outs'] = $score['scoreinfo']['outs'];
				$post['total'] = $post['ins'] + $post['outs'];
				$post['notes'] = $score['scoreinfo']['notes'];

				jimport('joomla.utilities.date');
				$date = new JDate($score['scoreinfo']['playdate']);
				$post['playdate'] = $date->toFormat('%d-%m-%Y');
			}
			$post['scoreid'] = $scoreid;
			$post['scores'] = $score['scores'];
		}

		if ($usergender === null) {
			$genders = array(array('value' => 0, 'text' => 'Male'), array('value' => 1, 'text' => 'Female'));
			$gender_select = JHTML::_('select.genericlist', $genders , 'usergender', 'onchange="onChangeGender()" class="inputbox"' . ' ', 'value', 'text', $post['usergender']);
			$this->assignRef('gender_select', $gender_select);
		}

		if (!isset($post['usergender'])) {
			$post['usergender'] = $usergender;
		}

		$this->assignRef('post', $post);
		parent::display($tmpl);
	}
}
?>
